<?php

namespace ary;

use Psr\Log\LoggerInterface;
use ShidianUtil\Net\IHttpClient;
use ShidianUtil\Utils\ConvertUtil;

/**
 * 案如云接口
 * Class Ary
 * @package ary
 * @see https://console-docs.apipost.cn/preview/828aadef618eed33/483f1f03c26f358a
 */
class Ary
{
    private IHttpClient $httpClient;
    private LoggerInterface $logger;

    public function __construct(IHttpClient $httpClient, LoggerInterface $logger)
    {
        $this->httpClient = $httpClient;
        $this->logger = $logger;
    }

    /**
     * 线索列表，按照ID获取
     * @param array $ids
     * @return AryResponse
     */
    public function getFavoriteList($params): AryResponse
    {

        $uri = 'http://crm.anruyun.com/xm/source/favoriteList';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 线索商机列表
     * @param $params
     * @return AryResponse
     */
    public function getSourceList($params): AryResponse
    {
        $uri = 'http://crm.anruyun.com/xm/source/businessLead';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 线索详情
     * @param $params
     * @return AryResponse
     */
    public function getSourceDetail($params): AryResponse
    {

        $uri = 'http://crm.anruyun.com/xm/source/detailInfo';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 案源-绑定虚拟号码
     * @param $params
     * @return AryResponse
     */
    public function bindSourcePhone($params): AryResponse
    {
        $uri = 'http://crm.anruyun.com/xm/source/bindAxb';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 咨询赚钱-绑定虚拟号码
     * @param $params
     * @return AryResponse
     */
    public function bindConsultingPhone($params): AryResponse
    {
        $uri = 'http://crm.anruyun.com/xm/source/bindAxbWithoutSource';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }


    /**
     * 买断
     * @param $params
     * @return AryResponse
     */
    public function buyout($params): AryResponse
    {
        $uri = 'http://crm.anruyun.com/xm/source/allin';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 下单
     * @param $params
     * @return AryResponse
     */
    public function buy($params):AryResponse{

        $uri = 'http://crm.anruyun.com/xm/source/buy';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 录音列表
     * @param $params
     * @return AryResponse
     */
    public function getRecordingList($params): AryResponse
    {
        $uri = 'http://crm.anruyun.com/xm/source/getPhoneBill';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 录音内容的文字列表
     * @param $params
     * @return AryResponse
     */
    public function getRecordingConvertRecordList($params): AryResponse
    {
        $uri = 'http://crm.anruyun.com/xm/source/phoneBillConvertRecord';

        $get = [];

        $headers = [];

        $post = $params;

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 案源类型-列表
     * @return AryResponse
     */
    public function getSourceTypeList(): AryResponse
    {
        $uri = 'http://crm.anruyun.com/xm/source/getSourceTypes';

        $get = [];

        $headers = [];

        $post = [];

        return $this->send($uri, $get, $headers, $post);
    }

    /**
     * 发出请求
     * @param string $uri
     * @param array $get
     * @param array $headers
     * @param array $post
     * @return AryResponse
     */
    private function send(string $uri, array $get, array $headers, array $post): AryResponse
    {
        $response = $this->httpClient->postFORMFormat($uri, $get, $headers, $post);

        $body = $response->getBody()->__toString();

        if ($response->getStatusCode() != 200) {
            $this->logger->error('案如云API接口HTTP状态码异常，响应{body},请求体{post}', ['body' => $body, 'post' => ConvertUtil::jsonEncode($post)]);
            return new AryResponse($response, false, []);
        }

        $data = [];

        try {
            $data = ConvertUtil::jsonDecode($body);

            if ($data['code'] === 200 || $data['code'] === '200') {
                $this->logger->info('案如云API接口正常，响应{body},请求体{post}', ['body' => $body, 'post' => ConvertUtil::jsonEncode($post)]);
                return new AryResponse($response, true, $data);
            }

            $this->logger->error('案如云API接口响应码错误，响应{body},请求体{post}', ['body' => $body, 'post' => ConvertUtil::jsonEncode($post)]);
            return new AryResponse($response, false, $data);
        } catch (\Throwable $throwable) {

            $this->logger->error('案如云API接口异常，原因{message}', ['message' => $throwable->getMessage()]);
            return new AryResponse($response, false, $data);
        }
    }

}
